﻿using Admin.Repository.Userinfos;
using Admin.Repository.UserRoles;
using Admin.UserRoles;
using AutoMapper;
using BLL.UserInfos.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Service.UserRoles
{
    public class UserRoleService : IUserRoleService
    {
        private readonly IUserRoleRepository roleRepository;

        private readonly IUserinfoRepository userinfoRepository;
        private readonly IMapper mapper;

        public UserRoleService(IUserRoleRepository roleRepository, IUserinfoRepository userinfoRepository,IMapper mapper)
        {
            this.roleRepository = roleRepository;
            this.userinfoRepository = userinfoRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 根据id查询一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RestDto<AddUserInfoDto> QueryId(int id)
        {
            var entity = userinfoRepository.QueryId(id);

            var data = mapper.Map<AddUserInfoDto>(entity);
            data.RoleId = roleRepository.QueryList(x => x.UserInfoId == entity.UserInfoId).Select(m => m.RoleId).ToArray();

            return new RestDto<AddUserInfoDto>
            {
                Code = 1,
                Data = data
                //new AddUserInfoDto
                //{
                //    UserInfoId = id,
                //    UserInfoPic = entity.UserInfoPic,
                //    Name = entity.Name,
                //    UserName = entity.UserName,
                //    UserPhone = entity.UserPhone,
                //    UserPassword = entity.UserPassword,
                //    ConfirmPassword = entity.UserPassword,
                //    Email = entity.Email,
                //    RoleId = roleRepository.QueryList(x => x.UserInfoId == entity.UserInfoId).Select(m => m.RoleId).ToArray()
                //}                
            };
        }
    }
}
